#include <57header.h>
#define BUFF_SIZE 1024*1024*4

long long current_time_millis() {
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts); // 获取当前时间
    return (long long)ts.tv_sec * 1000LL + ts.tv_nsec / 1000000; // 转换为毫秒
}

/* Usage: ./09_filecp src dest */
int main(int argc, char *argv[]){
    long long t1 = current_time_millis();
    ARGS_CHECK(argc, 3);
    int src_fd = open(argv[1], O_RDONLY);
    ERROR_CHECK(src_fd, -1, "open src");
    int dest_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
    ERROR_CHECK(dest_fd, -1, "open dest");

    char buf[BUFF_SIZE] = {0};
    int count;
    while((count = read(src_fd, buf, sizeof(buf))) > 0){
        write(dest_fd, buf, count);
    }
    
    close(src_fd);
    close(dest_fd);
    long long t2 = current_time_millis();
    printf("time = %lld\n", (t2 - t1));
    return 0;
}

